package com.aaa.behind.controller;


import com.aaa.behind.service.IEEmpInfoService;
import com.aaa.behind.service.IERoleService;
import com.aaa.entity.EEmpInfo;
import com.aaa.util.PageInfo;
import com.aaa.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 管理员信息表 前端控制器
 * </p>
 *
 * @author whj
 * @since 2023-01-13
 */
@RestController
@RequestMapping("/e-emp-info")
public class EEmpInfoController {

    @Autowired
    private IEEmpInfoService empInfoService;

    @Autowired
    private IERoleService roleService;

    @PostMapping("init")
    public Result init(@ModelAttribute PageInfo pageInfo, EEmpInfo eEmpInfo){
        return new Result(empInfoService.into(pageInfo,eEmpInfo));
    }

    @PostMapping("updateEmp")
    public Result updateEmp(EEmpInfo eEmpInfo){
        eEmpInfo.setGmtModified(LocalDateTime());
        return new Result<>(empInfoService.saveOrUpdate(eEmpInfo));
    }

    @PostMapping("insertEmp")
    public Result insertEmp(EEmpInfo eEmpInfo){
        eEmpInfo.setGmtCreate(LocalDateTime());
        eEmpInfo.setGmtModified(LocalDateTime());
        eEmpInfo.setIsDeleted(0);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        eEmpInfo.setPassword(bCryptPasswordEncoder.encode("123456"));
        eEmpInfo.setSalt("123");
        return new Result(empInfoService.saveOrUpdate(eEmpInfo));
    }

    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id){
        return new Result(empInfoService.removeById(id));
    }

    @PostMapping("findAll")
    public Result findAll(){
        return new Result(roleService.list());
    }


    @PostMapping("deleteById/{id}")
    public Result deleteById(@PathVariable String id){
        return new Result(empInfoService.removeById(id));
    }

    public String LocalDateTime(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        return format;
    }
}

